package com.wry.wry_test.algorithm;

import java.util.*;
import java.util.stream.Collectors;

/**
 * @Author: wangrongyi
 * @Date: 2024/11/26 14:11
 * @Description:
 */
public class Day16 {

    public static List<String> solution(int n, List<String> s, List<Integer> x) {
        // 实现插入排序降序
        int order = 0;
        Map<String, Node> map = new HashMap<>();
        for (int i = 0; i < n; i++) {
            String name = s.get(i);
            Integer value = x.get(i);
            if (map.containsKey(name)) {
                Node node = map.get(name);
                node.setValue(node.getValue() + value);
            } else {
                Node node = new Node(name, ++order, value);
                map.put(name, node);
            }
        }
        List<Node> nodes = new ArrayList<>(map.values().stream().toList());

        // 排序
        nodes.sort(new Comparator<Node>() {
            @Override
            public int compare(Node o1, Node o2) {
                if (o1.getValue() == o2.getValue()) {
                    return o1.getOrder() - o2.getOrder();
                }
                return o2.getValue() - o1.getValue();
            }
        });


        return nodes.stream().map(Node::getName).collect(Collectors.toList());
    }

    // 666
    static class Node {
        String name;
        int order;
        int value;
        Node(String name, int order, int value) {
            this.name = name;
            this.order = order;
            this.value = value;
        }
        Node() {}

        public int getValue() {
            return value;
        }

        public int getOrder() {
            return order;
        }

        public String getName() {
            return name;
        }

        public void setValue(int value) {
            this.value = value;
        }
    }

    public static void main(String[] args) {
        System.out.println(solution(4, Arrays.asList("a", "b", "c", "d"), Arrays.asList(1, 2, 2, 1)).equals(Arrays.asList("b", "c", "a", "d")));
        System.out.println(solution(3, Arrays.asList("x", "y", "z"), Arrays.asList(100, 200, 200)).equals(Arrays.asList("y", "z", "x")));
        System.out.println(solution(5, Arrays.asList("m", "n", "o", "p", "q"), Arrays.asList(50, 50, 30, 30, 20)).equals(Arrays.asList("m", "n", "o", "p", "q")));
    }
}
